package com.groupname.dangerousdelves;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.content.Intent;
import android.view.MotionEvent;
import sofia.app.ShapeScreen;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

// -------------------------------------------------------------------------
/**
 *  Write a one-sentence summary of your class here.
 *  Follow it with additional details about its purpose, what abstraction
 *  it represents, and how to use it.
 *
 *  @author Chris Fieldman (chrisf90)
 *  @version 2012.11.03
 */
public class DungeonScreen
    extends ShapeScreen implements OnCompletionListener
{
    //~-------------------------------------------------------------------------
    // Member Variables
    private Dungeon dungeon;
    public static CircularBuffer musicBuffer;
    public static DungeonScreen screen;

    //~-------------------------------------------------------------------------
    // Methods
    /**
     * Initialize the game and the GUI.
     */
    public void initialize()
    {
        screen = this;

        this.musicBuffer = new CircularBuffer(1);
        this.musicBuffer.enqueue(R.raw.toglorymono16);

        Intent music = new Intent(DangerousDelvesGame.context,
            SplashMusic.class);

        DangerousDelvesGame.context.stopService(music);

        DangerousDelvesGame.musicThread.stop();
        //DangerousDelvesGame.musicThread.destroy();
        DangerousDelvesGame.isPlayingSplashMusic = false;

        DangerousDelvesGame.musicThread = new Thread()
        {
            public void run()
            {
                DangerousDelvesGame.context = getApplicationContext();
                Intent bgmusic = new Intent(DangerousDelvesGame.context,
                    GameMusic.class);
                startService(bgmusic);
            }
        };

        DangerousDelvesGame.musicThread.start();

        this.dungeon = new Dungeon();
        this.dungeon.load("dungeon1");
    }

    // ----------------------------------------------------------
    /**
     * Place a description of your method here.
     * @param event
     */
    public void onTouchDown(MotionEvent event)
    {
        // temp
    }

    @Override
    public void onStop()
    {
        super.onStop();

        Intent music = new Intent(DangerousDelvesGame.context,
            GameMusic.class);

        DangerousDelvesGame.context.stopService(music);

        DangerousDelvesGame.musicThread.stop();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();

        Intent music = new Intent(DangerousDelvesGame.context,
            GameMusic.class);

        DangerousDelvesGame.context.stopService(music);

        DangerousDelvesGame.musicThread.stop();
    }

    public void onCompletion(MediaPlayer mp)
    {
        synchronized (this)
        {
            Intent music = new Intent(DangerousDelvesGame.context,
                GameMusic.class);

            DangerousDelvesGame.context.stopService(music);

            DangerousDelvesGame.musicThread.stop();

            DangerousDelvesGame.musicThread = new Thread()
            {
                public void run()
                {
                    DangerousDelvesGame.context = getApplicationContext();
                    Intent bgmusic = new Intent(DangerousDelvesGame.context,
                        GameMusic.class);
                    startService(bgmusic);
                }
            };

            DangerousDelvesGame.musicThread.start();
        }
    }
}